package team.bluepen.order.data.database.mysql;

import space.lingu.light.sql.AsciiSQLGenerator;
import space.lingu.light.sql.DialectProvider;
import space.lingu.light.sql.SQLGenerator;
import space.lingu.light.struct.DatabaseInfo;
import space.lingu.light.struct.Table;
import space.lingu.light.util.StringUtil;

/**
 * @author RollW
 */
public class MysqlDialectProvider implements DialectProvider {
    @Override
    public String create(Table table) {
        return null;
    }

    @Override
    public String create(DatabaseInfo info) {
        return null;
    }

    @Override
    public String destroyTable(String tableName) {
        return null;
    }

    @Override
    public String destroyDatabase(String databaseName) {
        return null;
    }

    @Override
    public String useDatabase(String databaseName) {
        return "use " + databaseName;
    }

    @Override
    public SQLGenerator getGenerator() {
        return MysqlSQLGenerator.INSTANCE;
    }

    public static class MysqlSQLGenerator extends AsciiSQLGenerator implements SQLGenerator {
        static final MysqlSQLGenerator INSTANCE = new MysqlSQLGenerator();

        @Override
        public String generateUpdate(String tableName, String... valueArgs) {
            if (StringUtil.isEmpty(tableName)) {
                return null;
            }
            StringBuilder builder = new StringBuilder("UPDATE ")
                    .append(escapeParam(tableName)).append(" SET (");
            for (int i = 0; i < valueArgs.length; i++) {
                if (i < valueArgs.length - 1) {
                    builder.append(escapeParam(valueArgs[i])).append("=?,");
                } else {
                    builder.append(escapeParam(valueArgs[i])).append("=?");
                }
            }
            builder.append(" WHERE ");
            return builder.toString();
        }
    }
}
